package com.bkgy.modular.tbdsjdatacollect.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.modular.tbdsjdatacollect.dao.TbDsjDataCollectDao;
import com.bkgy.modular.tbdsjdatacollect.entity.JTbDsjDataCollect;
import com.bkgy.modular.tbdsjdatacollect.entity.TbDsjDataCollect;
import com.bkgy.modular.tbdsjdatacollect.service.TbDsjDataCollectService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 数据采集表接口具体实现类
* @author  生成器生成
* @date 2022-07-26 15:56:37
*/
@Service
public class TbDsjDataCollectServiceImpl implements TbDsjDataCollectService {
	
	@Autowired
	private TbDsjDataCollectDao tbDsjDataCollectDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbDsjDataCollect(String data,Long userId,String sname){
		try{
			JTbDsjDataCollect jtbDsjDataCollect = JSON.parseObject(data,JTbDsjDataCollect.class);
    		TbDsjDataCollect tbDsjDataCollect = jtbDsjDataCollect.getTbDsjDataCollect();
    		CodiUtil.newRecord(userId,sname,tbDsjDataCollect);
            tbDsjDataCollectDao.insertDataTbDsjDataCollect(tbDsjDataCollect);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	}
	
	/**
	 * 根据主键删除对象
	 * @param
	 * */
	public ResultData deleteDataTbDsjDataCollectOne(String L1_VARIABLE_NAME,String out_data_source){
		try {
            tbDsjDataCollectDao.deleteDataTbDsjDataCollectOne(L1_VARIABLE_NAME,out_data_source);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	}
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjDataCollectMany(String str_id) {
        try {
        	String sql = "delete tb_dsj_data_collect where L1_VARIABLE_NAME in(" + str_id +")";
            tbDsjDataCollectDao.deleteDataTbDsjDataCollectMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbDsjDataCollect(String data,Long userId,String sname){
		try {
			JTbDsjDataCollect jtbDsjDataCollect = JSON.parseObject(data,JTbDsjDataCollect.class);
    		TbDsjDataCollect tbDsjDataCollect = jtbDsjDataCollect.getTbDsjDataCollect();
        	CodiUtil.editRecord(userId,sname,tbDsjDataCollect);
            tbDsjDataCollectDao.updateDataTbDsjDataCollect(tbDsjDataCollect);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	}
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjDataCollectByPage(String data) {
        try {
        	JTbDsjDataCollect jtbDsjDataCollect = JSON.parseObject(data, JTbDsjDataCollect.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbDsjDataCollect.getPageIndex();
        	Integer pageSize = jtbDsjDataCollect.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbDsjDataCollect.getCondition()){
    			jsonObject  = JSON.parseObject(jtbDsjDataCollect.getCondition().toString());
    		}
			QueryWrapper<TbDsjDataCollect> wrapper = new QueryWrapper<>();
			//变量描述
			String l1_variable_description = null;
			if (!StringUtils.isEmpty(jsonObject.get("l1_variable_description"))) {
				l1_variable_description = jsonObject.get("l1_variable_description").toString();
				wrapper.lambda().like(TbDsjDataCollect::getL1_variable_description,l1_variable_description);
			}
			//变量名
			String l1_variable_name = null;
			if (!StringUtils.isEmpty(jsonObject.get("l1_variable_name"))) {
				l1_variable_name = jsonObject.get("l1_variable_name").toString();
				wrapper.lambda().like(TbDsjDataCollect::getL1_variable_name,l1_variable_name);
			}
			//产线
			String production_line = null;
			if (!StringUtils.isEmpty(jsonObject.get("production_line"))) {
				production_line = jsonObject.get("production_line").toString();
				wrapper.lambda().like(TbDsjDataCollect::getProduction_line,production_line);
			}
			//数据源
			String out_data_source = null;
			if (!StringUtils.isEmpty(jsonObject.get("out_data_source"))) {
				out_data_source = jsonObject.get("out_data_source").toString();
				wrapper.lambda().eq(TbDsjDataCollect::getOut_data_source,out_data_source);
			}
			//表里别名
			String l1_variable_alias = null;
			if (!StringUtils.isEmpty(jsonObject.get("l1_variable_alias"))) {
				l1_variable_alias = jsonObject.get("l1_variable_alias").toString();
				wrapper.lambda().like(TbDsjDataCollect::getL1_variable_alias,l1_variable_alias);
			}
			Page<TbDsjDataCollect> page = new Page<>(pageIndex,pageSize);
			Page<TbDsjDataCollect> collect = tbDsjDataCollectDao.selectPage(page, wrapper);

			return ResultData.ResultDataSuccess(collect.getRecords(),(int)collect.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjDataCollectByIndocno(String data) {
        try {
        	JTbDsjDataCollect jtbDsjDataCollect = JSON.parseObject(data, JTbDsjDataCollect.class); 
        	String L1_VARIABLE_NAME = jtbDsjDataCollect.getL1_VARIABLE_NAME();
        	
    		TbDsjDataCollect tbDsjDataCollect = tbDsjDataCollectDao.findDataTbDsjDataCollectByIndocno(L1_VARIABLE_NAME);
    		return ResultData.ResultDataSuccess(tbDsjDataCollect);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbDsjDataCollect> findDataTbDsjDataCollect(){
		List<TbDsjDataCollect> list = tbDsjDataCollectDao.findDataTbDsjDataCollect();
		return list;
	}
}
